package edu.mit.compilers.lowercode.code;

import edu.mit.compilers.lowercode.Operand;
import edu.mit.compilers.lowercode.Variable;

public class AssignSingleOperandCode extends AssignCode {
    // unary op like "-" "!", may be null
    private String unaryOp;

    // increment op: "+" or "-", may be null
    private String incrementOp;

    private Operand right;

    public AssignSingleOperandCode(Variable left, Operand location, Operand right, String unaryOp, String incrementOp) {
        super(left, location);
        this.right = right;
        this.unaryOp = unaryOp;
        this.incrementOp = incrementOp;
        assert unaryOp == null || incrementOp == null;
    }

    public AssignSingleOperandCode(Variable left, Operand location, Operand right, String unaryOp) {
        this(left, location, right, unaryOp, null);
    }

    public AssignSingleOperandCode(Variable left, Operand right, String unaryOp) {
        this(left, null, right, unaryOp);
    }

    public AssignSingleOperandCode(Variable left, Operand right) {
        this(left, right, null);
    }

    public Operand getRight() {
        return right;
    }

    public boolean isUnaryOp() {
        return unaryOp != null;
    }

    public String getUnaryOp() {
        return unaryOp;
    }

    public boolean isIncrementOp() { return incrementOp != null; }

    public String getIncrementOp() { return incrementOp; }

    @Override
    protected String getStringFroShow(String prefix) {
        return prefix
                + getLeftString() + " = "
                + (isUnaryOp() ? (unaryOp + " ") : (""))
                + right.getLiteralString();
    }

    @Override
    public <T> T accept(ThreadAddressCodeVistor<T> visitor) {
        return visitor.visit(this);
    }
}
